﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using zy6.Models;

namespace zy6.Controllers
{
    public class DefaultController : Controller
    {
        Model1 model = new Model1();
        // GET: Default
        public ActionResult Index()
        {//查询
            var list = model.Task.ToList();
            return View(list);
        }
        public ActionResult Detail(int id)
        {//详情
            Task task = model.Task.FirstOrDefault(p => p.TaskID == id);
            return View(task);
        }
        [HttpPost]
        public ActionResult Delete(int id)
        {//删除
            Task task = model.Task.FirstOrDefault(p => p.TaskID == id);
            if (task != null)
            {
                model.Task.Remove(task);
                var result = model.SaveChanges() > 0;
                return Json(result);
            }
            return Json(false);
        }
       /// <summary>
       /// 添加
       /// </summary>
       /// <returns></returns>
        public ActionResult Create() {
            return View();

        }
        [HttpPost]
        public ActionResult Create(Task task)
        {
            if (ModelState.IsValid)
            {
                model.Task.Add(task);
                var result = model.SaveChanges();
                if (result > 0)
                {
                    return RedirectToAction("Index");

                }
            }
            return View(task);
        }
        public ActionResult Update(int id)
        {
            var task = model.Task.FirstOrDefault(p=>p.TaskID==id);
            return View(task);
        }
        [HttpPost]
            public ActionResult Update(Task task) 
        {
            if (ModelState.IsValid) {
                var list = model.Task.Where(p => p.TaskID == task.TaskID).FirstOrDefault();
                if (list!=null) {
                    list.TaskID = task.TaskID;
                    list.TaskPriority = task.TaskPriority;
                    list.Content = task.Content;
                    list.AddTime = task.AddTime;
                    list.TaskState = task.TaskState;
                    list.FinishedTime = task.FinishedTime;
                    list.uid = task.uid;
                    var u = model.SaveChanges();
                    if (u>0) {
                        return RedirectToAction("Index");
                    
                    }
                }
            }
            return View(task);

        }
    }
}